package org.shoukaiseki.common.utils;

import java.lang.reflect.Array;
import java.util.List;

/**
 * spring参数转换工具类
 **/
public class SpringParamUtils {


    //string参数转为Long数组
    public static Long[] stringToArrayLong(String str) {
        List<Long> longList = NumberUtils.stringToListLong(str, StringUtils.SEPARATOR_COMMA);
        return longList.stream().toArray(Long[]::new);
    }


    //spring 参数转换方式
    public static <T> T[] stringToArrayLongConverter(String source,Class<? extends Number > targetType){
        String[] fields = StringUtils.split(source,StringUtils.SEPARATOR_COMMA);
        Object target = Array.newInstance(targetType, fields.length);
        for (int i = 0; i < fields.length; i++) {
            String sourceElement = fields[i];
            Object targetElement = org.springframework.util.NumberUtils.parseNumber(sourceElement.trim(), targetType);
            Array.set(target, i, targetElement);
        }
        return (T[]) target;

    }
}
